//
//  BRInviteRewardViewController.m
//  BrezzeSaaS
//
//  Created by IOS on 2022/1/21.
//  Copyright © 2022 Brezze GWG. All rights reserved.
//

#import "BRInviteRewardViewController.h"
#import "BRInviteRewardView.h"
#import "BRInviteRewardModel.h"
@interface BRInviteRewardViewController ()
{
    UIView *_contentView;
}
@end

@implementation BRInviteRewardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.emptyLYView setImage:UIImageMake(@"error_invitation")];
    [self setUI];
    [self requestRule];
}

#pragma mark - Request --
- (void)requestRule {
    
    [[BRRequestManager shared] GET:[BRAPI inviteReward] params:@{} succeed:^(NSDictionary * _Nonnull dataDict, NSInteger respCode) {
        
        NSLog(@"reward -- %@",dataDict);
        
        NSArray *arr = [BRInviteRewardModel mj_objectArrayWithKeyValuesArray:dataDict[@"data"]];
        [self createContentView:arr];
        
    } failed:^(NSInteger code, NSString * _Nonnull errMsg) {
        
    }];
    
}

#pragma mark - SET UI --
- (void)setUI {
    UIView *top = [self createTop:Localized(@"Invite_invitationReward")];
    top.backgroundColor = UIColor.qd_backgroundColor;
    self.titleColor = UIColor.qd_titleTextColor;
    
    
    UIScrollView *sView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:sView];
    [sView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(top.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    
    UIView *bView = [[UIView alloc] init];
    bView.backgroundColor = UIColorClear;
    [sView addSubview:bView];
    [bView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(sView);
        make.width.mas_equalTo(kScreenWidth);
    }];
    _contentView = bView;
    
}

- (void)createContentView:(NSArray *)arr {
    [_contentView.subviews respondsToSelector:@selector(removeFromSuperview)];
    if (arr.count == 0) {
        
        [_contentView addSubview:self.emptyLYView];
        [self.emptyLYView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self->_contentView);
        }];
        
        return;
    }
    __block BRInviteRewardView *baseView;
    for (int i = 0; i < arr.count; i++) {
        BRInviteRewardModel *dto = arr[i];
        
        BRInviteRewardView *view = [[BRInviteRewardView alloc] init];
        [_contentView addSubview:view];
        view.dto = dto;
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
                 
            if (baseView != nil) {
                make.top.equalTo(baseView.mas_bottom).offset(12);
            }else {
                make.top.offset(12);
            }
            if (i == arr.count - 1) {
                make.bottom.equalTo(self->_contentView.mas_bottom).offset(-(12 + kSafeAreaBottomHeight));
            }
            make.left.offset(12);
            make.right.offset(-12);
        }];
        baseView = view;
    }
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
